#!/usr/bin/env bash

[[ $UID -ne 0 ]] && echo "This program must be run as root!" && exit 1

InstallConfig() {
    rm ~/.vim ~/.vimrc ~/.vimrc.bundles -rf
    cp vim ~/.vim -R
    cp vimrc ~/.vimrc
    cp vimrc.bundles ~/.vimrc.bundles
}

InstallVim() {
    PYTHON3DIR="";
    if [[ -z $PYTHON3DIR ]]; then
        echo -n "Please input your python3 dir: "
        read PYTHON3DIR
    fi
    if ! [[ -d $PYTHON3DIR ]]; then
        echo "Didn't found this path"
        return 1
    fi
    cd vim-master
    export PYTHON3DIR; \
    export PATH=$PYTHON3DIR/bin:$PATH; \
    export PYTHON3=$PYTHON3DIR/bin/python; \
    export PY3LIBDIR=$($PYTHON3 -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))"); \
    export PY3CONFDIR=$(${PYTHON3}-config --configdir); \
    export LDFLAGS="-L$PY3LIBDIR -Wl,-rpath,$PY3LIBDIR"; \
    ./configure --enable-python3interp=yes --with-python3-config-dir=$PY3CONFDIR; \
    make; \
    make install; \
    make clean; \
    make distclean
    cd ..
}

InstallAll() {
    InstallVim
    InstallConfig
}


# Main
Tips='
Welcome to use vim installer, select option please:
1. Only install configuration.
2. Only build vim.
3. Build vim and install configuration.
4. Quit and exit.
Your selection:  '

echo -e "\033[32m\c"
echo "$Tips"
echo -e "\033[1A\033[18C\033[0m\c"
read OPTION

START=$(date +%s)

case "$OPTION" in
    1)
        ACTION="InstallConfig"
        ;;
    2)
        ACTION="InstallVim"
        ;;
    3)
        ACTION="InstallAll"
        ;;
    4)
        ;;
    *)
        echo "Didn't match anything"
        ACTION=""
esac

END=$(date +%s)

if ! [[ -z $ACTION ]]; then
    $ACTION

    if [[ $? -eq 0 ]]; then
        INFO="
Finish $ACTION, thanks for using vim installer, enjoy it now. :-).
"
        echo -e "\033[32m\c"
        echo "$INFO"
        echo -e "\033[0m\c"
    fi
fi
